package setting

import (
	"../common"
	"../web"
	"strconv"
	"strings"
)

type SetRoutingObj struct {
	common.BaseHandler
}

type RoutingQueryObj struct {
	common.BaseHandler
}

func (self *SetRoutingObj) Prepare() {
	stations := *common.FetchAllRowsPtr(
		`SELECT STATION, ''
           FROM SFC_STATION(NOLOCK) 
          ORDER BY STATION`,
		self.Usr.Conn, new(common.Station))

	routs := *common.FetchAllRowsPtr(
		`SELECT ROW_NUMBER() OVER(ORDER BY NAME), NAME
          FROM SFC_ROUTING(NOLOCK) GROUP BY NAME`,
		self.Usr.Conn, new(common.RouteIndex))

	rprpro := *common.FetchAllRowsPtr(
		`SELECT DISTINCT PROCESS 
           FROM SFC_REPAIRSETTING(NOLOCK) 
          ORDER BY PROCESS`,
		self.Usr.Conn, new(common.RecDataStr))

	self.TmplData["stations"] = stations
	self.TmplData["routs"] = routs
	self.TmplData["rprpro"] = rprpro
}

func (self *SetRoutingObj) ParameterCheck() bool {
	c_name := strings.TrimSpace(self.Ctx.Params["routname"])
	c_curr := self.Ctx.Params["currstation"]
	c_next := self.Ctx.Params["nextstation"]
	c_rprs := self.Ctx.Params["repairstation"]
	c_cycl := self.Ctx.Params["cyclestation"]
	c_byps := self.Ctx.Params["bypass"]
	c_test := self.Ctx.Params["musttest"]
	if c_name == "" {
		self.TmplData["error"] = common.GetText(self.Lang, "Please input the routing name.")
		return false
	}
	if c_curr == "" {
		self.TmplData["error"] = common.GetText(self.Lang, "Please select the current station.")
		return false
	}
	if c_next == "" {
		self.TmplData["error"] = common.GetText(self.Lang, "Please select the next station.")
		return false
	}
	if c_curr == c_next {
		self.TmplData["error"] = common.GetText(self.Lang, "The current station and next station must be different.")
		return false
	}
	if strings.TrimSpace(c_next) == "[END]" {
		c_next = "SFCF"
	}

	i_byps, _ := strconv.Atoi(c_byps)
	i_test, _ := strconv.Atoi(c_test)

	self.Sqlparas = append(self.Sqlparas, c_name)
	self.Sqlparas = append(self.Sqlparas, c_curr)
	self.Sqlparas = append(self.Sqlparas, c_next)
	self.Sqlparas = append(self.Sqlparas, c_rprs)
	self.Sqlparas = append(self.Sqlparas, c_cycl)
	self.Sqlparas = append(self.Sqlparas, i_byps)
	self.Sqlparas = append(self.Sqlparas, i_test)
	self.Sqlparas = append(self.Sqlparas, self.Usr.Username)
	self.Flashparas = []interface{}{c_name, c_curr}

	return true
}

func (self *SetRoutingObj) SqlExecute() bool {
	spRtnData := *common.FetchOneRowPtr(
		`sp_SetRouting ?, ?, ?, ?, ?, ?, ?, ?`,
		self.Usr.Conn, new(common.SPReturn), self.Sqlparas...)
	if spRtnData.(common.SPReturn).Flag > 0 {
		self.TmplData["error"] = common.GetTextSP(self.Lang, spRtnData)
		return false
	}
	return true
}

func RoutSetHandler(ctx *web.Context) {
	self := new(SetRoutingObj)
	self.Ctx = ctx
	self.Url = "/setrouting"
	self.Html = "setrouting.html"
	self.Rightlist = []string{"setrouting"}
	self.Flashstr = "Set Routing: [ %v ] Station: [ %v ] finished."
	self.Do(self)
}

func (self *RoutingQueryObj) ParameterCheck() bool {
	s_rout := self.Ctx.Params["q"]

	self.Sqlparas = append(self.Sqlparas, s_rout)

	return true
}

func (self *RoutingQueryObj) SqlQueryExecute() {
	routs := *common.FetchAllRowsPtr(
		`SELECT ROW_NUMBER() OVER(ORDER BY IDX),PREV_STATION,CURR_STATION,NEXT_STATION,
				 CYCLE_STATION,REPAIR_STATION,CASE BYPASS WHEN 1 THEN 'Y' ELSE 'N' END,
				 CASE MUSTBETEST WHEN 1 THEN 'Y' ELSE 'N' END
           FROM SFC_ROUTING(NOLOCK) 
          WHERE NAME=?`,
		self.Usr.Conn, new(common.RouteInfo), self.Sqlparas...)
	self.TmplData["routs"] = routs
	self.TmplData["routname"] = self.Sqlparas[0].(string)
}

func GetRoutingHandler(ctx *web.Context) {
	self := new(RoutingQueryObj)
	self.Ctx = ctx
	self.Html = "routingdetail.html"
	self.DoQueryUrl(self)
}
